<!-- 打烊时间配置 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form :label-col="{ span: 5 }" :wrapper-col="{ span: 17 }">
                <a-form-item label="所在仓库">
                    <a-select v-model="storehouseId" placeholder="请选择" @change="getData">
                        <a-select-option v-for="(itm) in storeList" :value="itm.storehouseId" :key="itm.storehouseId">{{itm.storehouseName}}</a-select-option>
                    </a-select>       
                </a-form-item>
                <a-form-item label="打烊开始时间" >
                    <a-time-picker v-model="startTime" format="HH:mm:ss" valueFormat="HH:mm:ss" allowClear/>
                </a-form-item>
                <a-form-item label="打烊结束时间" >
                    <a-time-picker v-model="endTime" format="HH:mm:ss" valueFormat="HH:mm:ss" allowClear/>
                </a-form-item>
                <a-form-item label="状态">
                    <a-switch checked-children="开启" un-checked-children="关闭" :checked="status == '1' ? true : false" @change="onChange"/>
                </a-form-item>
                <a-form-item v-bind="formItemLayoutWithOutLabel">
                    <a-button type="primary" @click="submit">确定</a-button>
                </a-form-item>
            </a-form>
        </div>  
    </div>
</template>

<script>
import { queryGoodsCloseConfig , updateGoodsCloseConfig } from '@/api/brand';
import { queryGoodsStorehouseBindConfig , queryGoodsStorehouseBindConfigByUserid } from '@/api/newMall';
export default {
    data () {
        return {
            startTime: '', // 打烊开始时间
            endTime: '', // 打烊结束时间
            status: '1', // 状态
            brandId: this.global.brandId,
            id: '',
            formItemLayoutWithOutLabel: {
                wrapperCol: {
                    xs: { span: 24, offset: 0 },
                    sm: { span: 20, offset: 6 },
                },
            },
            storehouseId: undefined,
            storeList: [],
            userId: sessionStorage.getItem('userId'),
        };
    },
    async created(){
        try {
            await this._queryGoodsStorehouseBindConfig();
            this.getData();
        } catch (error) {
            this.$notification['error']({
                message: '提示',
                description: error,
                duration: 8,
            })
        }
    },
    methods: {
        // 查询用户所包含的仓库信息
        _queryGoodsStorehouseBindConfig(){
            let _this = this;
            return new Promise((resolve,reject) => {
                queryGoodsStorehouseBindConfigByUserid({userId: _this.userId,size: 99,brandId: _this.brandId})
                .then(res => {
                    if(res.success){
                        _this.storeList = res.data;
                        _this.storehouseId = res.data.length > 0 ? res.data[0].storehouseId : '';
                        resolve();
                    }else{
                        reject(res.errMessage);
                    }
                })
            })
        },
        getData(){
            let _this = this;
            const params = {
                page: 0,
                size: 20,
                'storehouseId': _this.storehouseId,
            }
            queryGoodsCloseConfig(params)
            .then(res => {
                if(res.success){
                    if(res.data.content.length > 0){
                        _this.startTime = res.data.content[0].startTime;
                        _this.endTime = res.data.content[0].endTime;
                        _this.status = res.data.content[0].status;
                        _this.id = res.data.content[0].id;
                    }else{
                        _this.startTime = '';
                        _this.endTime = '';
                        _this.status = '';
                        _this.id = '';
                    }
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        submit(){
            let _this = this;
            const params = {
                'id': _this.id,
                'startTime': _this.startTime,
                'endTime': _this.endTime,
                'storehouseId': _this.storehouseId,
                'status': _this.status,
                'brandId': _this.brandId,
            }
            updateGoodsCloseConfig(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '修改成功',
                        duration: 8,
                    })
                    _this.getData();
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        onChange(checked) {
            console.log(`a-switch to ${checked}`);
            this.status = checked ? '1' : '0'
        },
    }
}

</script>
<style scoped>

</style>